package silo.dungeonbuddy;

import java.util.Vector;

public class Terrain extends FlatImage
{
	private int row;
	private int column;
	private int height;
	private boolean obstruction;
	
	public Terrain(String eName, Start game,String ref, String IP, int eRow, int eCol, int eSizeX, int eSizeY, int eHeight, boolean eObstruction)
	{
		super(eName,ref,game,eRow*(int)Math.floor(game.getHeight()/20),eCol*(int)Math.floor(game.getHeight()/20),eSizeX,eSizeY);
		Vector v = (Vector)game.getBoard().getSquareList().get(eRow);
		Square s = (Square)v.get(eCol);
		x=s.getX();
		y=s.getY();
		row = eRow;
		column = eCol;
		obstruction=eObstruction;
		height=eHeight;
	}//constructors
	
	//TODO: ensure that the squares' occupees vector manipulation will work with the move subs below:
	
	public void moveTo(Square s)
	{
		for(int i =0; i<sizeX; i++)
		{
			for(int j =0; j<sizeY; j++)
			{
				Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
				Square sq = (Square)v.get(column+i);
				sq.getOccupees().remove(this);
			}//next j
		}//next i
		row=s.getRow();
		column=s.getColumn();
		Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
		Square sq2 = (Square)v2.get(column);
		x=sq2.getX();
		y=sq2.getY();
		for(int i =0; i<sizeX; i++)
		{
			for(int j =0; j<sizeY; j++)
			{
				Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
				Square sq = (Square)v.get(column+i);
				sq.getOccupees().add(this);
			}//next j
		}//next i
	}//moveTo
	
	public void moveUp()
	{
		if(row>0)
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveDown()
	{
		if(row<game.getBoard().getHeight())
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveLeft()
	{
		if(column>0)
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column-1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveRight()
	{
		if(column<game.getBoard().getHeight())
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column+1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveUpLeft()
	{
		if((row>0)&&(column>0))
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column-1;
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveDownLeft()
	{
		if((row<game.getBoard().getHeight())&&(column>0))
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column-1;
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveUpRight()
	{
		if((row>0)&&(column<game.getBoard().getWidth()))
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column+1;
			row=row-1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	public void moveDownRight()
	{
		if((row<game.getBoard().getHeight())&&(column<game.getBoard().getWidth()))
		{
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			column=column+1;
			row=row+1;
			//square=game.getBoard().getSquareList()[row][column];
			Vector v2 = (Vector)game.getBoard().getSquareList().get(row);
			Square sq2 = (Square)v2.get(column);
			x=sq2.getX();
			y=sq2.getY();
			for(int i =0; i<sizeX; i++)
			{
				for(int j =0; j<sizeY; j++)
				{
					Vector v = (Vector)game.getBoard().getSquareList().get(row+j);
					Square sq = (Square)v.get(column+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}
	}
	
	//GETTERS
	public int getRow()
	{
		return row;
	}
	public int getColumn()
	{
		return column;
	}
	public int getHeight()
	{
		return height;
	}
	public boolean getObstruction()
	{
		return obstruction;
	}
	
	//SETTERS
	public void setHeight(int eHeight)
	{
		height=eHeight;
	}
	public void setObstruction(boolean eObstruction)
	{
		obstruction=eObstruction;
	}
	
	//toString
	public String toString()
	{
		if(visible)
		{
			if(obstruction)
			{
				return "2" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + height + "," + "1" + "," + row + "," + column + "," + "1" + ",";
			}else
			{
				return "2" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + height + "," + "1" + "," + row + "," + column + "," + "0" + ",";
			}
		}else
		{
			if(obstruction)
			{
				return "2" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + height + "," + "0" + "," + row + "," + column + "," + "1" + ",";
			}else
			{
				return "2" + "," + x + "," + y + "," + name + "," + spriteRef + "," + sizeX + "," + sizeY + "," + height + "," + "0" + "," + row + "," + column + "," + "0" + ",";
			}
		}
	}//toString
}//class Terrain